<?php 
/**
*	文件下载预览
*/
class fileClassAction extends ActionNot
{
	public $userinfo = array();
	public $agentkey = '';
	
	public function initAction()
	{
		$key = $this->get('key');
		if(isempt($key))exit('url is error1');
		$da  = c('cache')->get($key);
		if(!$da)exit('地址已经失效了');
		
		$fileid  		= $da['fileid'];
		$websign 		= $da['websign'];
		$this->agentkey = $da['agentkey'];
		$this->userinfo = $da['userinfo'];
		$nowsign		= md5($this->rock->HTTPweb);
		//if($nowsign!=$websign)exit('无效的访问');
		
		$this->frs 		= m('file')->getone($fileid);
		$this->fileid 	= $fileid;
		if(!$this->frs)exit('record not found');
		$filepath 	 = $this->frs['filepath'];
		$showpath 	 = $this->frs['filepath'];
		$filepathout = $this->frs['filepathout'];
		if(isempt($filepathout) && !file_exists($filepath))exit('文件不存在的');
		if($filepathout && !file_exists($filepath))$showpath = $filepathout;
		$this->frs['showpath']  = $showpath;
		$this->smartydata['key']= $key;
		$this->assign('frs', $this->frs);
	}
	
	/**
	*	在线编辑的
	*/
	public function editAction()
	{
		$fileext = $this->frs['fileext'];
		$office  = ',docx,doc,xls,xlsx,ppt,pptx,';
		$exiss  = ','.$fileext.',';
		$edit 	= getconfig('officebj_edit');
		if(contain($office, $exiss)){
			if($edit=='1'){
				$this->onlyoffice_view(0);//onlyoffice编辑
			}else{
				$torul = $this->wps_view(0);
				if(!$torul){
					$this->displayfile = ''.P.'/file/view_office.html';
					$this->smartydata['otype']=0;//编辑
				}
			}
		}else{
			$this->view_open();
		}
	}
	
	/**
	*	预览文件
	*/
	public function defaultAction()
	{
		$fileext	= $this->frs['fileext'];
		$pdfpath	= $this->frs['pdfpath'];
		if($fileext=='pdf'){
			return $this->view_pdf($this->frs['filepath']);
		}else{
			if(!isempt($pdfpath) && file_exists($pdfpath))return $this->view_pdf($pdfpath);
		}
		$exiss  = ','.$fileext.',';
		$office = ',docx,doc,xls,xlsx,ppt,pptx,';
		$imgext = ',gif,png,jpeg,jpg,';
		
		if(contain($office, $exiss)){
			$this->view_office();
		}else if(contain($imgext, $exiss)){
			$this->view_image();
		}else if($fileext=='mp4'){	
			$this->view_mp4();
		}else{
			$this->view_open();
		}
	}
	private function view_pdf($filepath){
		$this->displayfile = ''.P.'/file/view_pdf.html';
		$filepathout	= $this->frs['filepathout'];
		if(!file_exists($filepath) && !isempt($filepathout)){
			$this->rock->createtxt($filepath, file_get_contents($filepathout));
		}
		$this->assign('filepath', $this->jm->base64encode($filepath));
	}
	private function view_open(){
		$this->displayfile = ''.P.'/file/view_open.html';
		$content 	= '';
		$filepath	= $this->frs['showpath'];
		$content	= file_get_contents($filepath);
		if(substr($filepath,-4)=='temp'){
			$content	= base64_decode($content);
		}
		$this->smartydata['content']=$content;
	}
	private function view_image(){
		$this->displayfile = ''.P.'/file/view_image.html';
	}
	private function view_mp4(){
		$this->displayfile = ''.P.'/file/view_mp4.html';
	}
	private function view_office(){
		$fan = getconfig('officebj_fan');
		$filepathout	= $this->frs['filepathout'];
		if($fan==2 || $fan==3){
			$filepath	= $this->rock->getouturl().$this->frs['filepath'];
			if(!isempt($filepathout))$filepath = $filepathout;
			$url = 'https://view.officeapps.live.com/op/view.aspx?src='.urlencode($filepath).'';
			//if($fan==3)$url = 'https://docview.mingdao.com/op/view.aspx?src='.urlencode($filepath).'';
			$this->rock->location($url);
		}else if($fan==4){
			$this->displayfile = ''.P.'/file/view_topdf.html';
		}else{
			if($fan==1){
				$this->onlyoffice_view(1);
			}else{
				$torul = $this->wps_view(1);
				if(!$torul){
					$this->displayfile = ''.P.'/file/view_office.html';
					$this->smartydata['otype']=1;
				}
			}
		}
	}
	
	
	/**
	*	下载
	*/
	public function downAction()
	{
		$this->display = false;
		m('file')->update('`downci`=`downci`+1', $this->fileid);
		$size		= floatval($this->frs['filesize']);
		$filename	= $this->frs['filename'];
		$fileext	= $this->frs['fileext'];
		$filepath	= $this->frs['filepath'];
		$filepathout	= $this->frs['filepathout'];
		
		if(!file_exists($filepath) && !isempt($filepathout) && $size>5*1024*1024){
			$this->rock->location($filepathout);
			return;
		}
		
		$filename 	= $this->iconvutf8(str_replace(' ','',$filename));
		header('Content-type:application/octet-stream');
		header('Accept-Ranges: bytes');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: no-cache');
		if($size>0)header('Content-Length:'.$size.'');
		header('Expires: 0');
		header('Content-disposition:attachment;filename='.$filename.'');
		header('Content-Transfer-Encoding: binary');
		ob_clean();
		flush();
		
		if(!file_exists($filepath) && !isempt($filepathout)){
			readfile($filepathout);
			return;
		}
		if(substr($filepath,-4)=='temp'){
			$content	= file_get_contents($filepath);
			return base64_decode($content);
		}else{
			readfile($filepath);
		}
	}
	
	//渣渣IE才需要转化，真是醉了
	private function iconvutf8($text) {
		if(contain($this->rock->web,'IE')){
			return iconv('utf-8','gb2312', $text);
		}else{
			return $text;
		} 
    }
	
	
	
	
	
	
	
	
	
	/**
	*	在线编辑平台发送预览
	*/
	public function sendeditAction()
	{
		$otype = (int)$this->get('otype','1');
		return c('rockedit')->sendedit($this->fileid,$this->userinfo, $otype);
	}
	
	/**
	*	请求转化
	*/
	public function changetopdfAction()
	{
		return c('xinhuapi')->officesend($this->fileid);
	}
	
	/**
	*	获取状态
	*/
	public function officestatusAction()
	{
		return c('xinhuapi')->officestatus($this->fileid);
	}
	
	/**
	*	下载文件
	*/
	public function officedownAction()
	{
		return c('xinhuapi')->officedown($this->fileid);
	}
	
	//0编辑,1预览
	public function wps_view($otype)
	{
		$wpsapp_id  = getconfig('wpsapp_id');
		$wpsapp_key = getconfig('wpsapp_key');
		if(!$wpsapp_id || !$wpsapp_key)return '';
		
		$systype = getconfig('systype');
		if(!$systype)$systype='online';
		$tourl = c('wps')->geturl($this->frs, array(
			'_w_yingkey' => $this->agentkey,
			'_w_agentkey'=> 'xh0hx0xh0sgx0xx0eh0kk0ak0xh0sii0kk0ah0hk0ha0xa0sgx0xx0sge0hs0ng0xx0xs0hk0sig0xk0hx0xa0sgk0kg0hx0hn0sis0xx0eh0xg0sgk0xx0sii0hi0sgx0hk0sgk0xj0nh02',
			'_w_filenum' => $this->frs['filenum'],
			'_w_cfroace' => $systype,
			'_w_optid'	 => $this->userinfo['id'],
			'_w_permission' => ($otype==0) ? 'write' : 'read',
		));
		$this->rock->location($tourl);
		return 'ok';
	}
	
	/**
	*	only预览
	*/
	public function onlyoffice_view($otype)
	{
		$this->displayfile = ''.P.'/file/view_onlyoffice.html';
		$frs = $this->frs;
		
		$fileurl		 = $frs['filepath'];
		$callbackUrl	 = '';
		if(!isempt($frs['filepathout']))$fileurl = $frs['filepathout'];
		if(substr($fileurl,0,4)!='http')$fileurl = $this->rock->getouturl().$fileurl;
		
		$frs['viewtype'] 	 = $this->rock->ismobile() ? 'mobile' : 'desktop';
		$frs['documentType'] = $this->getdocumentType($frs['fileext']);
		$frs['fileurl']	 	 = $fileurl;
		
		//编辑就需要回调
		if($otype==0){
			$callbackUrl = $this->rock->getouturl().'api.php?m=onlyoffice&a=editcall&agentkey='.$this->agentkey.'&filenum='.$frs['filenum'].'&optid='.$this->userinfo['id'].'&optname='.$this->jm->base64encode($this->userinfo['name']).'';
			$callbackUrl = "'+jm.base64decode('".$this->jm->base64encode($callbackUrl)."')+'";
		}

		$frs['mode'] 		 = ($otype==0) ? 'edit' : 'view';
		$frs['filekey'] 	 = '';
		$this->assign('frs', $frs);
		$this->assign('optrs', $this->userinfo);
		$this->assign('callbackUrl', $callbackUrl);
		
		$onlyurl = getconfig('onlyoffice_url');
		if(isempt($onlyurl))$onlyurl = $this->rock->jm->base64decode('aHR0cDovLzEwNi41My4yMjIuNDU6ODA4MC8:');
		if(substr($onlyurl,-1)!='/')$onlyurl.='/';
		$this->assign('onlyurl', $onlyurl);
	}
	private function getdocumentType($filext)
	{
		$lx = 'text';
		$xles = explode(',', 'xls,xlsx,ods,csv');
		$ppps = explode(',', 'ppt,pptx,odp,csv');
		if(in_array($filext, $xles))$lx='spreadsheet';
		if(in_array($filext, $ppps))$lx='presentation';
		return $lx;
	}
	
	//http://127.0.0.1/app/rockfile/api.php/v1/3rd/file/info?_w_agentkey=xl8dtmbi7m&_w_appid=b477b66238f8452b8336665729086274&_w_callurl=aHR0cDovL29hLnJvY2tvYS5jb20vYXBpLnBocD9tPXVwbG9hZCZhPXVwZmlsZXZiJmZpbGVpZD01NSZhZG1pbmlkPTEmdG9rZW49ajY3a2ZscGk%3A&_w_fname=%E4%B8%AA%E7%A8%8E%E7%94%B3%E6%8A%A5%E9%87%91%E9%A2%9D.xlsx&_w_optid=1&_w_permission=write&_w_signature=KX3TxWC2ZQUblS%2B81JoyAE7ILSk%3D&access_token=
}